import Vue from "vue";
import Vuex from 'vuex';
import {subjectList} from "../util/request/subject";
import {is_empty} from "../util/funcutils";

Vue.use(Vuex)

export default new Vuex.Store({
  // 数据
  state: {
    subjects: [],
    finishUpdate: {id: '', bz: ''}
  },
  //控制
  actions: {
    initSubjects(context) {
      subjectList({
        limit: 0,
        offset: 20
      }).then(res => {
        context.commit('setSubjects', res.data.data.list)
      })
    },
    setSubjects(context, value) {
      context.commit('setSubjects', value)
    },

    setFinishUpdate(context, value) {
      context.commit('setFinishUpdate', value)
    },
    checkToken() {
      let token = localStorage.getItem('token');
      if (is_empty(token)) {
        this.$bus.$emit('logout')
        this.$router.push('/login')
      }
    }
  },
  // 修改
  mutations: {
    setSubjects(state, value) {
      state.subjects = value
    },
    setFinishUpdate(state, value) {
      state.finishUpdate = value
    }
  },
  getters: {},
})
